from flask import Blueprint
from flask_restful import Api, Resource, reqparse, marshal, fields, marshal_with

from apps.models.search_record import Search
from exts import db

first_bp = Blueprint("first", __name__)
api = Api(first_bp)

search_fields = {
    "date_time": fields.String,  # 直接使用datetime类型返回的数据类型不好处理，直接设置为string
    "search_word": fields.String
}


class DataApi(Resource):
    @marshal_with(search_fields)
    def get(self):
        datas = Search.query.all()
        return datas


class FirstApi(Resource):
    def get(self):
        searches = Search.query.all()
        for search in searches:
            a = search
        # 遍历到最后一项获取时间
        return {"time": str(a.date_time)}


api.add_resource(FirstApi, "/first")
api.add_resource(DataApi, '/data_first')
